﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;

namespace YArchitech.Revit
{
	public class PlumbingSystemUpdater : ElementsUpdater
	{
		public PlumbingSystemUpdater(Autodesk.Revit.DB.Document doc, List<HYUpdateElementInfo> listNodes) : base(doc, listNodes)
		{
		}

		protected override bool Update(string docTitle)
		{
			bool flag = false;
			List<HYUpdateElementInfo> list = new List<HYUpdateElementInfo>();
			Transaction transaction = new Transaction(this.document, "更新数据");
			transaction.Start();
			try
			{
				foreach (HYUpdateElementInfo hyupdateElementInfo in this.nodesToUpdate)
				{
					if (!(hyupdateElementInfo.DocumentTitle == docTitle))
					{
						base.FailedString = "图元所属文档与当前文档不匹配";
						flag = false;
						break;
					}
					Element element = this.document.GetElement(new ElementId(hyupdateElementInfo.ElementID));
					if (element != null)
					{
						if (base.CheckCategory(element, BuiltInCategory.OST_PipeCurves))
						{
							flag &= base.UpdatePipe(element, hyupdateElementInfo);
						}
						else if (base.CheckCategory(element, BuiltInCategory.OST_Sprinklers))
						{
							list.Add(hyupdateElementInfo);
						}
					}
				}
				this.document.Regenerate();
				flag = ((int)transaction.Commit() == 3);
				foreach (HYUpdateElementInfo hyupdateElementInfo2 in list)
				{
					Element element2 = this.document.GetElement(new ElementId(hyupdateElementInfo2.ElementID));
					if (element2 != null)
					{
						flag &= PlumbingSystemUpdater.WriteFlowToSprinkler(element2, hyupdateElementInfo2.FLow);
					}
				}
			}
			catch (Exception ex)
			{
				base.FailedString = ex.ToString();
				flag = false;
			}
			finally
			{
				if (transaction != null && transaction.HasStarted() && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
					flag = false;
				}
			}
			return flag;
		}

		private static Schema CreateSprinklerSchema(Autodesk.Revit.DB.Document doc, Guid schemaGuid, double obj)
		{
			Schema schema = Schema.Lookup(schemaGuid);
			if (schema == null)
			{
				Transaction transaction = new Transaction(doc, "CreateSprinlerSchema");
				try
				{
					transaction.Start();
					SchemaBuilder schemaBuilder = new SchemaBuilder(schemaGuid);
					schemaBuilder.SetReadAccessLevel((AccessLevel)1);
					schemaBuilder.SetWriteAccessLevel((AccessLevel)1);
					schemaBuilder.SetSchemaName("SprinklerExtensibleData");
                    schemaBuilder.AddSimpleField("SprinklerFlow", typeof(double)).SetUnitType((UnitType)37);
					schema = schemaBuilder.Finish();
					transaction.Commit();
				}
				catch
				{
					transaction.RollBack();
				}
			}
			return schema;
		}

		public static bool WriteFlowToSprinkler(Element elem, double flow)
		{
			bool result = false;
			Document document = elem.Document;
			Transaction transaction = new Transaction(document, "WriteExtensibleStorage");
			Schema schema = PlumbingSystemUpdater.CreateSprinklerSchema(document, PlumbingSystemUpdater.guidOfSprinklerSchema, flow);
			if (schema != null && schema.SchemaName == "SprinklerExtensibleData")
			{
				try
				{
					transaction.Start();
					Entity entity = new Entity(schema);
					Field field = schema.GetField("SprinklerFlow");
					entity.Set<double>(field, flow, (Autodesk.Revit.DB.DisplayUnitType)64);
					elem.SetEntity(entity);
					transaction.Commit();
					result = true;
				}
				catch
				{
					transaction.RollBack();
				}
			}
			return result;
		}

		public static double ReadFlowFromSrpinkler(Element elem)
		{
			double result = 0.0;
			Document document = elem.Document;
			Schema schema = Schema.Lookup(PlumbingSystemUpdater.guidOfSprinklerSchema);
			if (schema != null && schema.SchemaName == "SprinklerExtensibleData")
			{
				try
				{
					Entity entity = elem.GetEntity(schema);
					if (entity != null && entity.IsValid())
					{
						result = entity.Get<double>(schema.GetField("SprinklerFlow"), (Autodesk.Revit.DB.DisplayUnitType)64);
					}
				}
				catch
				{
				}
			}
			return result;
		}

		public static readonly Guid guidOfSprinklerSchema = new Guid("FCD17400-66EF-442C-A5ED-24953123520F");
	}
}
